package cz.byteworks.android.myway.util;

import java.util.Timer;
import java.util.TimerTask;

import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.view.View;
import android.widget.RemoteViews;
import cz.byteworks.android.myway.activities.R;
import cz.byteworks.android.myway.location.AutoLocator;
import cz.byteworks.android.myway.widget.MyWayWidget;

public class AutoButtonTextTask extends TimerTask {

	private Context ctx;
	private Timer timer;
	private int count;
	
	public AutoButtonTextTask(Context ctx, Timer timer) {
		this.ctx = ctx;
		this.timer = timer;
		count = 0;
	}
	
	@Override
	public void run() {
		count++;
		if (AutoLocator.isAutolocationRunning()) {
			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ctx);
			ComponentName thisWidget = new ComponentName(ctx, MyWayWidget.class);
			RemoteViews views = new RemoteViews(ctx.getPackageName(), R.layout.widget);
			views.setViewVisibility(R.id.button_auto_location, View.GONE);
			views.setViewVisibility(R.id.button_auto_location_act, View.VISIBLE);
			appWidgetManager.updateAppWidget(thisWidget, views);
		}
		else {
			AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(ctx);
			ComponentName thisWidget = new ComponentName(ctx, MyWayWidget.class);
			RemoteViews views = new RemoteViews(ctx.getPackageName(), R.layout.widget);
			views.setViewVisibility(R.id.button_auto_location_act, View.GONE);
			views.setViewVisibility(R.id.button_auto_location, View.VISIBLE);
			appWidgetManager.updateAppWidget(thisWidget, views);
		}
		if (count >= 20) {
			timer.cancel();
		}
	}

}
